package com.in28minutes.example.spring.examples;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.InitialContext;
import javax.sql.DataSource;
//Employees
//DataSource => Db Connection
public class OldJdbcExample {
public void printEmployees() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// Get a connection from data source
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/HSQLDS");
conn = ds.getConnection("hsqlusername", "hsqlpassword");
stmt = conn.createStatement();
String sql = "SELECT id, name, age FROM Employee";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
int age = rs.getInt("age");
String name = rs.getString("name");
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", Name: " + name);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}